package com.zl.service.Impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zl.common.Result;
import com.zl.dto.JournalDto;
import com.zl.entity.JournalEntity;
import com.zl.mapper.JournalMapper;
import com.zl.service.IJournalService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class IJournalServiceImpl extends ServiceImpl<JournalMapper, JournalEntity> implements IJournalService {
    @Autowired
    private JournalMapper journalMapper;




    @Override
    public int insert(JournalDto journalDto ) {
        JournalEntity journalEntity = new JournalEntity();
        BeanUtils.copyProperties(journalDto,journalEntity);
        JournalEntity res = getById(journalEntity.getId());
        if(res!=null){
            return 0;
        }
        boolean save = save(journalEntity);
        if (!save)
            return 0;
        return 1;
    }

    @Override
    public List<JournalEntity> listByIsDisplay() {
        List<JournalEntity> list = lambdaQuery().list();
        if(CollUtil.isEmpty(list))
            return Collections.emptyList();
        return list;
    }

    @Override
    public List<JournalEntity> getByTitle(String title) {
        return List.of();
    }


    @Override
    public Result selectByCondition(String title, String authors, String pubDate) {
        List<JournalEntity> list = lambdaQuery()
                .like(title != null, JournalEntity::getTitle, title)
                .like(authors != null, JournalEntity::getAuthor, authors)
                .like(pubDate != null, JournalEntity::getPubDate, pubDate)
                .list();
        if (CollUtil.isEmpty(list))
            return Result.ok(Collections.emptyList());
        else
            return Result.ok(list);
    }

}
